/*
 * libustream: User-space stackable streams
 * @(#) $Id: fd.c 2 2009-02-20 09:52:11Z mo@ilaven.net $
 */

/*
 * Copyright (c) 2002, 2008 Mo McRoberts.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) of this software may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_libustream.h"

/* Return the stream for the given file descriptor */
static IUStream *
us__fd_stream(int fd)
{
	return NULL;
}

/* Return the file descriptor associated with a given stream */
static int
us__fd_lookup(IUStream *stream)
{
	return -1;
}

int
us__fd_associate(int fd, IUStream *us)
{
/*	fprintf(stderr, "Associating stream with fd %d\n", fd); */
	return 0;
}

int
us__fd_disassociate(int fd, IUStream *us)
{
/*	fprintf(stderr, "Disassociating stream from fd %d\n", fd); */
	return 0;
}

/* us_open()
 * Open a stream, returning its file descriptor.
 */
int
us_open(const char *iri, int oflag, ...)
{
	va_list ap;
	mode_t mode = 0;
	IUStream *stream;
	
	if(O_CREAT == (oflag & O_CREAT))
	{
		va_start(ap, oflag);
		mode = (mode_t) va_arg(ap, int);
	}
	if(NULL == (stream = us_create(iri, oflag, mode)))
	{
		return -1;
	}
	return us__fd_lookup(stream);
}

/* us_creat()
 * Equivalent of us_open(iri, O_CREAT|O_TRUNC|O_WRONLY, mode)
 */
int
us_creat(const char *iri, mode_t mode)
{
	return us_open(iri, O_CREAT|O_TRUNC|O_WRONLY, mode);
}

/* us_write()
 * Write @nbyte bytes from the buffer @buf to the stream identified by the
 * descriptor @fd, returning the number of bytes actually written on success,
 * or -1 on error.
 */
ssize_t
us_write(int fd, const void *buf, size_t nbyte)
{
	IUStream *stream;
	int r;
	size_t nb;
	
	if(NULL == (stream = us__fd_stream(fd)))
	{
		errno = EBADF;
		return -1;
	}
	nb = nbyte;
	r = IUStream_Write(stream, buf, &nb);
	IUStream_Release(stream);
	if(COM_S_OK != r)
	{
		errno = r;
		return -1;
	}
	return (ssize_t) (nbyte - nb);
}

/* us_read()
 * Read up to @nbyte bytes into the buffer @buf from the stream identified
 * by the descriptor @fd, returning the number of bytes actually read on
 * success or -1 on error.
 * Because UStream socket connections support receiving packets consisting
 * of metadata but no actual payload, us_read will return -1 and set
 * errno to EPIPE on end-of-file, rather than simply returning zero. A
 * return value of zero just means that there was no payload contained
 * in this packet.
 */

ssize_t
us_read(int fd, const void *buf, size_t nbyte)
{
	IUStream *stream;
	int r;
	size_t nb;

	if(NULL == (stream = us__fd_stream(fd)))
	{
		errno = EBADF;
		return -1;
	}
	nb = nbyte;
	r = IUStream_Read(stream, buf, &nb);
	IUStream_Release(stream);
	if(COM_S_OK != r)
	{
		if(EPIPE == r)
		{
			return 0;
		}
		errno = r;
		return -1;
	}
	return (ssize_t) (nbyte - nb);
}

/* us_close()
 * Destroy the stream associated with the descriptor @fd
 */
int
us_close(int fd)
{
	IUStream *stream;
	
	if(NULL == (stream = us__fd_stream(fd)))
	{
		errno = EBADF;
		return -1;
	}
	/* Release it twice; once because us__fd_stream calls AddRef(),
	 * and again to actually close the stream.
	 */
	IUStream_Release(stream);
	IUStream_Release(stream);
	return 0;
}
